package com.kq.tallybookl.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kq.tallybookl.R;
import com.kq.tallybookl.database.FakeManager;
import com.kq.tallybookl.item.MenoItem;

import java.util.List;

/**
 * Created by Kq on 2016/4/29.
 */
public class TestActivity extends Activity {

    private List<MenoItem> list;

    private TextView textView;
    private EditText ed_title,ed_details;
    private Button btn_add,btn_update,btn_delete,btn_delete_all;

    private FakeManager fakeManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //SQLiteDatabase.loadLibs(this); //first init the db libraries with the context
//        FakeDBHelper.getWritableDatabase("thisismysecret");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        fakeManager = new FakeManager(this);

        textView = (TextView) findViewById(R.id.textView);
        ed_title = (EditText) findViewById(R.id.editText_title);
        ed_details = (EditText) findViewById(R.id.editText_details);

        btn_add = (Button) findViewById(R.id.button_add);
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fakeManager.addMenoItem(new MenoItem(ed_title.getText().toString(),
                        System.currentTimeMillis(),ed_details.getText().toString()));
                list = fakeManager.buildMenoItems();
                String pass = fakeManager.getLoginPassword();
                if(pass!=null){
                    Log.i("pass",pass);
                }else {
                    fakeManager.addLoginPassword("pass22222");
                }
                logAll();
            }
        });
        btn_delete = (Button) findViewById(R.id.button_delete);
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = fakeManager.buildMenoItems();
                fakeManager.deleteMenoItem(list.get(0));
                fakeManager.updateLoginPassword("pass22222","pass111");
                logAll();
            }
        });
        btn_update = (Button) findViewById(R.id.button_update);
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(fakeManager.hasMenoItems()) {
                    list = fakeManager.buildMenoItems();
                    fakeManager.updateMenoItemTitle(list.get(0), "sha");
                    fakeManager.updateMenoItemDetails(list.get(0), "5555566555");
                    fakeManager.updateMenoItemTimestamp(list.get(0),
                            String.valueOf(System.currentTimeMillis()));
                    logAll();
                }
                if(fakeManager.hasLoginPassword()){
                    Log.i("pass","has pass");
                }else{
                    Log.i("pass","hasn't pass");
                }
            }
        });
        btn_delete_all = (Button) findViewById(R.id.button_delete_all);
        btn_delete_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fakeManager.clearAll();
                logAll();
            }
        });
    }

    private void logAll(){
        list = fakeManager.buildMenoItems();
        if (list!=null){
            for(MenoItem item :list){
                Log.i("item",item.getTitle());
                Log.i("item",item.getDetails());
                Log.i("item",item.getTimestamp()+"");
            }
        }else {
            Log.i("list","list = null");
        }
    }
}
